package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.util.List;


//web 建立 BookServlet 承接请求,走serviceImpe,到bookDao,执行sql.
public class BookServlet extends BaseServlet {
    BookService bookService=new BookServiceImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //List<Book> list=bookService.queryBooks();
        //request.setAttribute("list",list);
        //request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
        response.sendRedirect("bookServlet?action=page&pageNo=1");
    }

    public void editBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getParameter("id")!=null){//这是update行为
            System.out.println("BookServlet 29:进入了修改环节");
            System.out.println("BookServlet 30:id is:"+request.getParameter("id"));
            request.setAttribute("id",request.getParameter("id"));
            Book book=bookService.queryBookById(WebUtils.parseInt(request.getParameter("id"),-1));
            request.setAttribute("book",book);
            String page=request.getParameter("page");
            request.setAttribute("page",page);
            request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
        }else{
            System.out.println("BookServlet 34:进入了添加环节");
            String page=request.getParameter("page");
            request.setAttribute("page",page);
            request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
        }
    }

    public void doAddBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book=WebUtils.CopyParamToBean(new Book(),request.getParameterMap());
        if(!request.getParameter("id").equals("")){//这是update行为
            //Book book=WebUtils.CopyParamToBean(new Book(),request.getParameterMap());
            System.out.println("BookServlet 47:id is:"+request.getParameter("id"));
            bookService.updateBook(book);
            //response.sendRedirect("bookServlet?action=list");
            String page=request.getParameter("page");
            response.sendRedirect("bookServlet?action=page&pageNo="+page);
        }else{//这是add行为
            //request.setCharacterEncoding("UTF-8");
            //System.out.println(request.getParameter("name"));
            //Book book=WebUtils.CopyParamToBean(new Book(),request.getParameterMap());
            bookService.addBook(book);
            //response.sendRedirect("bookServlet?action=list");
            String page=request.getParameter("page");
            response.sendRedirect("bookServlet?action=page&pageNo="+page);
        }
        //response.sendRedirect("bookServlet?action=list");
        //response.sendRedirect("bookServlet?action=page&pageNo=1");
    }

    public void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idString=request.getParameter("id");
        Integer id=WebUtils.parseInt(idString,-1);
        System.out.println("BookServlet 63:id is:"+id);
        bookService.deleteBookById(id);
        //response.sendRedirect("bookServlet?action=list");
        String page=request.getParameter("page");
        response.sendRedirect("bookServlet?action=page&pageNo="+page);
    }
    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNoString=request.getParameter("pageNo");//当前页码
        Integer pageNo=WebUtils.parseInt(pageNoString,1);
        System.out.println("BookServlet 68:pageNo is:"+pageNo);

        //String pageSizeString=request.getParameter("pageNo");//数据条数
        //Integer pageSize=WebUtils.parseInt(pageSizeString, Page.PAGE_SIZE);//默认为4
        //System.out.println("BookServlet 72:pageSize is:"+pageSize);
        Integer pageSize=Page.PAGE_SIZE;

        Page page=bookService.page(pageNo,pageSize);
        if(pageNo<1){//防止用户输入越界
            response.sendRedirect("bookServlet?action=page&pageNo=1");
        }
        if(pageNo>page.getPageNo()){
            response.sendRedirect("bookServlet?action=page&pageNo="+page.getPageNo());
        }
        page.setUrl("manager/bookServlet?action=page");//这个参数,被用在"跳转页地址url生成"时,action是page时,采用得到;也就是有分页需求时才用得到
        request.setAttribute("page",page);
        //response.sendRedirect("bookServlet?action=list");
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(Thread.currentThread().getStackTrace()[1].getFileName()+":"
                +Thread.currentThread().getStackTrace()[1].getLineNumber()+":"
                +"已经进入doget方法,"+request.getParameter("action")+" 是action名");
        doPost(request,response);//把dopost方法优化到baseServlet抽象类中去了,使得可以按照request action执行特定的方法
        //由于action判断行为在dopost中,所以在doget中执行一下dopost就可以执行判断了,从而执行特定的方法了.
    }
}
